package com.fone.player.online.adapter;

import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.client.Reporter;
import com.fone.player.entity.RedirectInfo;
import com.fone.player.play.PlayerFrom;
import com.fone.player.util.Activity2poster;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class PostersAdapter extends PagerAdapter {

	/** 日志标签 */
	private static final String TAG = "PostersAdapter";

	private String shost;
	private List<RedirectInfo> posterList;
	// private List<View> viewList;
	// private ImageLoader imageLoader;
	private Context context;

	DisplayImageOptions options;

	LayoutInflater inflater;
	int mComefrom;

	public PostersAdapter(Context context, Boolean isHot, String shost, List<RedirectInfo> posterList, int comefrom) {
		super();
		this.context = context;
		this.shost = shost;
		this.posterList = posterList;
		
		mComefrom = comefrom;

		if (isHot) {
			options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.default_720_420).showImageForEmptyUri(R.drawable.default_720_420).showImageOnFail(R.drawable.default_720_420).cacheInMemory(true).cacheOnDisk(true).considerExifParams(true).build();
		} else {
			options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.default_720_252).showImageForEmptyUri(R.drawable.default_720_252).showImageOnFail(R.drawable.default_720_252).cacheInMemory(true).cacheOnDisk(true).considerExifParams(true).build();
		}

		// viewList = new ArrayList<View>();
		initData();
	}

	private void initData() {
		// viewList.clear();
		inflater = LayoutInflater.from(context);
		// for (int i = 0; i < posterList.size(); i++) {
		// View view = inflater.inflate(R.layout.item_posters, null);
		// viewList.add(view);
		// }

	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {

		return arg0 == arg1;
	}

	@Override
	public int getCount() {

		return posterList.size();
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);

	}

	@Override
	public int getItemPosition(Object object) {

		return super.getItemPosition(object);
	}

	@Override
	public CharSequence getPageTitle(int position) {

		return null;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		final RedirectInfo info = posterList.get(position);

		View v = container.findViewById(position);
		ViewHolder holder = null;
		if (v == null) {
			v = inflater.inflate(R.layout.item_posters, null);
			v.setId(position);
			holder = new ViewHolder((ImageView) v.findViewById(R.posters.pic), (TextView) v.findViewById(R.posters.title));
			container.addView(v);
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}

		setContent(v, holder, info);

		return v;
	}

	private class ViewHolder {
		public ImageView pic;
		public TextView title;

		public ViewHolder(ImageView pic, TextView title) {
			super();
			this.pic = pic;
			this.title = title;
		}
	}

	private void setContent(View v, ViewHolder holder, final RedirectInfo info) {
		holder.title.setText(info.name);
		// 设置图片
		// if (!TextUtils.isEmpty(info.pic) && info.pic.length() > 7) {
		// ImageDownloadModule.getInstance().download(FoneUtil.getAbsoluteUrl("",
		// shost, info.pic), R.drawable.default_160, pic);

		ImageLoader.getInstance().displayImage(FoneUtil.getAbsoluteUrl("", shost, info.pic), holder.pic, options);
		// }

		v.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (FoneUtil.isNetOkWithToast(context)) {
					Log.i("PostersAdapter", "PostersAdapter onClick ");
					L.v(TAG, "instantiateItem", info.toString());
					//Reporter.logEvent(PlayerFrom.BANNER_COLLECTION);
					Activity2poster.startActivity(context, info, mComefrom);
				}
			}
		});
	}

	@Override
	public void notifyDataSetChanged() {
		// TODO Auto-generated method stub
		super.notifyDataSetChanged();
		initData();
	}

}
